<?php
/***************************************************************************
 *							  avatar_manage.php
 *							-------------------
 *   begin				: Thursday, Apr 25, 2002
 *
 ***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/
/* Modifications made by CPG Dev Team http://cpgnuke.com				*/
/************************************************************************/
if (!defined('ADMIN_PAGES')) { exit; }
// Any mode passed?
if( isset($_GET['mode']) || isset($_POST['mode']) ) {
	$mode = ( isset($_GET['mode']) ) ? $_GET['mode'] : $_POST['mode'];
	$target = ( isset($_GET['target']) ) ? $_GET['target'] : $_POST['target'];
} else {
	$mode = "";
}

// Read in the board config to maintain dynamic
$result = $db->query('SELECT config_name, config_value FROM '. CONFIG_TABLE);
while ($row = $result->fetch_row()) {
	$board_config[$row[0]] = $row[1];
}

// Select all avatars and usernames that have an uploaded avatar currently
$result = $db->query("SELECT user_avatar, username FROM " . USERS_TABLE
	 . " WHERE user_avatar_type = " . USER_AVATAR_UPLOAD . " AND user_avatar IS NOT NULL");
// Create a hash to keep track of all the user that is using the uploaded avatar
while ($row = $result->fetch_row()) {
	$avatar_usage[$row[0]] = $row[1];
}

// This is the variable that points to the path of the avatars
// You may need to adjust this to meet your needs ;)
$real_avatar_dir = $MAIN_CFG['avatar']['path'];

switch ($mode)
{
	case "delete":
		echo '<table cellpadding="4" cellspacing="1" border="0" class="forumline">';
		if ( unlink($real_avatar_dir.'/'.$target) ) {
			echo "<tr><td>Success, $target deleted!</td></tr><tr><td><a href=\"".URL::admin("&amp;do=avatar")."\">Continue</a></td></tr></table>";
		} else {
			echo "<tr><td>FAILED to delete $target!</td></tr><tr><td><a href=\"javascript:history.go(-1)\">Go Back</a></td></tr></table>";
		}
		break;

	default:
		$template->set_filenames(array('body' => 'admin/forums/avatars'));

		$template->avatars = array();

		// This is where we go through the avatar directory and report whether they are not
		// used or if they are used, by who.
		if ($avatar_dir = opendir($real_avatar_dir)) {
			Dragonfly::getKernel()->L10N->load('Your_Account');

			$avatars = array();
			while ($file = readdir($avatar_dir)) {
				// This is where the script will filter out any file that doesn't match the patterns
				if ($file != "." && $file != ".." && preg_match("#\.(gif|jpg|jpeg|png)$#",$file)) {
					$avatars[] = $file;
				}
			}
			$count = count($avatars);

			$start = (int)$_GET->uint('start');
			$limit = 40;

			sort($avatars);
			$avatars = array_slice($avatars, $start, $limit);
			foreach ($avatars as $file)
			{
				$size = filesize($real_avatar_dir.'/'.$file);
				// Alternating row colows code
				$delete_url = $edit_url = null;
				if (isset($avatar_usage[$file]) ) {
					// Bingo, someone is using this avatar
					// Since we need to supply a link with a valid sid later in html, let's build it now
					$av_id = $avatar_usage[$file];
					list($av_uid) = $db->uFetchRow("SELECT user_id FROM " . USERS_TABLE . " WHERE username = '{$av_id}'");
					$username = $avatar_usage[$file];
					$edit_url = URL::admin("users&amp;mode=edit&amp;edit=avatar&amp;id={$av_uid}");
				} else {
					// Not used, safe to display delete link for admin
					$username = _NONE;
					$delete_url = URL::admin("&amp;do=avatar&amp;mode=delete&target={$file}");
				}
				$template->avatars[] = array(
					'AVATAR_IMG' => "$real_avatar_dir/$file",
					'FILENAME' => $file,
					'SIZE' => $size,
					'USERNAME' => $username,
					'EDIT' => $edit_url,
					'DELETE' => $delete_url,
				);
			}

			$template->avatars_pagination = generate_admin_pagination('&amp;do=avatar', $count, $limit, $start);

		} else {
			// If we made it to this else there was a problem trying to read the avatar directory
			// If you see this error message check this variable:
			// $real_avatar_dir -> This may be set incorrectly for your site.
			echo "Avatar directory unavailable!";
		}
		break;
}
